<!--留言板模块-->
<script src="/javascripts/angular.min.js"></script>
<%if(logined){%>
<input type="hidden" value="<%=userInfo._id%>" id="uid"/>

<%}%>
<div class="msgBoard" ng-app="webApp" ng-controller="msgBoard">
    <div class="row">

        <%messageList.forEach(function(item){%>
        <%var targetAuthor;if(item.utype && item.utype == '1'){targetAuthor = item.adminAuthor}else{targetAuthor = item.author}%>

        <div class="col-md-12 col-sm-12 msgList">
            <div class="col-md-1 media-logo">
                <img src="<%=targetAuthor.logo%>" class="img-responsive" alt="<%=targetAuthor.comments%>"/>
            </div>
            <div class="col-md-11">
                <h3 class="media-heading">
                    <a href="###" class="uName"><%=targetAuthor.userName%></a>
                    <%if(item.utype && item.utype == '1'){%>
                    <span title="管理员" style="color: #0da3e2;font-size: 12px;">[<i class="fa fa-user"></i>&nbsp;管理员]</span>
                    <%}%>
                    <span><small><%=myDateFormat(item.date)%></small><a href="###" ng-click="replyMsg('<%=item._id%>','<%=targetAuthor._id%>','<%=targetAuthor.userName%>','<%=targetAuthor.email%>')" class="pull-right reply">回复</a></span>
                </h3>
                <%if(item.replyAuthor){%>
                <%var beReply = '<span style="color: #0da3e2">@'+item.replyAuthor.userName+'</span>&nbsp;'%>
                <p><%-beReply%><%=item.content%></p>
                <%}else{%>
                <p><%-item.content%></p>
                <%}%>
            </div>
        </div>
        <%})%>
    </div>
    <div class="row">
        <div class="col-md-12 col-sm-12 giveMsg">
            <h3>添加新评论</h3>
            <div class="txt-area">
                <form role="form" class="form-horizontal" name="msgForm" ng-submit="sentMsgForm(msgForm.$valid)" novalidate>
                    <div class="">
                        <textarea rows="3" class="form-control" name="content" id="msgTextArea" ng-model="msgFormData.content"  ng-minlength="10" ng-maxlength="500" required></textarea>
                        <label for="inputError" class="control-label text-danger" ng-show="msgForm.content.$invalid && !msgForm.content.$pristine"><i class="glyphicon glyphicon-info-sign"></i> 10-500个非特殊字符</label>
                    </div>
                    <p class="sent-btn">
                        <button class="btn btn-default theme-btn pull-right"  ng-disabled="msgForm.$invalid">发表评论</button>
                    </p>
                </form>
            </div>
        </div>
    </div>

</div>

<script>
    var doraApp = angular.module("webApp",[]);
    doraApp.controller('msgBoard',function($scope,$http){

                $scope.msgFormData = {};
                var loginState = '<%=logined%>';
                $scope.msgFormData.contentId = '<%=documentInfo._id%>';
                $scope.msgFormData.contentTitle = '<%=documentInfo.title%>';

                $scope.sentMsgForm = function(isValid){

                    if(!loginState){
                        alert('请先登录!');
                        window.location = '/users/login';
                    }else{

                        $scope.msgFormData.utype = '0';
                        var oldContent = $scope.msgFormData.content;
						if($scope.msgFormData.replyUserName){
							$scope.msgFormData.content = oldContent.substring($scope.msgFormData.replyUserName.length+1,oldContent.length);
						}

                        $http({
                            method  : 'POST',
                            url     : '/users/message/sent',
                            data    : $.param($scope.msgFormData),  // pass in data as strings
                            headers : { 'Content-Type': 'application/x-www-form-urlencoded' }  // set the headers so angular passing info as form data (not request payload)
                        })
                        .success(function(data) {
                            if(data === "success"){
                                window.location.reload();
                            }else{
                                alert(data);
                            }
                        });
                    }

                };

//                回复留言
                $scope.replyMsg = function(msgId,authorId,authorName,authorEmail){

                    if(!loginState){
                        alert('请先登录!');
                        window.location = '/users/login';
                    }else{

                        $('#msgTextArea').focus();
                        $scope.msgFormData.content = "@"+authorName+" ";
                        $scope.msgFormData.replyUserName = authorName;
                        $scope.msgFormData.replyEmail = authorEmail;
                        $scope.msgFormData.replyId = authorId;
                        $scope.msgFormData.relationMsgId = msgId;

                    }
                }
     });

</script>